<template>
  <el-dialog :title="form.ruleId ? '编辑' : '新增'" v-model="visible"
             :close-on-click-modal="false" draggable>
    <el-form ref="dataFormRef" :model="form" :rules="dataRules" formDialogRef label-width="90px" v-loading="loading">
      <el-row :gutter="24">
        <el-col :span="12" class="mb20">
          <el-form-item label="规则编码" prop="ruleCode">
            <el-input v-model="form.ruleCode" placeholder="请输入规则编码"/>
          </el-form-item>
        </el-col>

        <el-col :span="12" class="mb20">
          <el-form-item label="规则名称" prop="ruleName">
            <el-input v-model="form.ruleName" placeholder="请输入规则名称"/>
          </el-form-item>
        </el-col>

        <el-col :span="12" class="mb20">
          <el-form-item label="描述" prop="ruleDesc">
            <el-input v-model="form.ruleDesc" placeholder="请输入描述"/>
          </el-form-item>
        </el-col>

        <el-col :span="12" class="mb20">
          <el-form-item label="最大长度" prop="maxLength">
            <el-input v-model="form.maxLength" placeholder="请输入最大长度"/>
          </el-form-item>
        </el-col>

        <el-col :span="12" class="mb20">
          <el-form-item label="是否补齐" prop="isPadded">
            <el-input v-model="form.isPadded" placeholder="请输入是否补齐"/>
          </el-form-item>
        </el-col>

        <el-col :span="12" class="mb20">
          <el-form-item label="是否启用" prop="enableFlag">
            <el-select v-model="form.enableFlag" placeholder="请选择是否启用">
              <el-option :value="item.value" :label="item.label" v-for="(item, index) in YES_OR_NOT" :key="index"></el-option>
            </el-select>
          </el-form-item>
        </el-col>

      </el-row>
    </el-form>
    <template #footer>
        <span class="dialog-footer">
          <el-button @click="visible = false">取消</el-button>
          <el-button type="primary" @click="onSubmit" :disabled="loading">确认</el-button>
        </span>
    </template>
  </el-dialog>
</template>

<script setup lang="ts" name="SysAutoCodeRuleDialog">
import { useDict } from '/@/hooks/dict';
import { useMessage } from "/@/hooks/message";
import { getObj, addObj, putObj } from '/@/api/autocode/sysAutoCodeRule'
import { rule } from '/@/utils/validate';
const emit = defineEmits(['refresh']);

// 定义变量内容
const dataFormRef = ref();
const visible = ref(false)
const loading = ref(false)
// 定义字典
const { YES_OR_NOT } = useDict('YES_OR_NOT')

// 提交表单数据
const form = reactive({
  ruleId:'',
  ruleCode: '',
  ruleName: '',
  ruleDesc: '',
  maxLength: '',
  isPadded: '',
  enableFlag: '',
});

// 定义校验规则
const dataRules = ref({
  ruleCode: [{required: true, message: '规则编码不能为空', trigger: 'blur'}],
  ruleName: [{required: true, message: '规则名称不能为空', trigger: 'blur'}],
  ruleDesc: [{required: true, message: '描述不能为空', trigger: 'blur'}],
  maxLength: [{required: true, message: '最大长度不能为空', trigger: 'blur'}],
  isPadded: [{required: true, message: '是否补齐不能为空', trigger: 'blur'}],
  enableFlag: [{required: true, message: '是否启用不能为空', trigger: 'blur'}],
})

// 打开弹窗
const openDialog = (id: string) => {
  visible.value = true
  form.ruleId = ''

  // 重置表单数据
  nextTick(() => {
    dataFormRef.value?.resetFields();
  });

  // 获取sysAutoCodeRule信息
  if (id) {
    form.ruleId = id
    getsysAutoCodeRuleData(id)
  }
};

// 提交
const onSubmit = async () => {
  const valid = await dataFormRef.value.validate().catch(() => {});
  if (!valid) return false;

  try {
    loading.value = true;
    form.ruleId ? await putObj(form) : await addObj(form);
    useMessage().success(form.ruleId ? '修改成功' : '添加成功');
    visible.value = false;
    emit('refresh');
  } catch (err: any) {
    useMessage().error(err.msg);
  } finally {
    loading.value = false;
  }
};


// 初始化表单数据
const getsysAutoCodeRuleData = (id: string) => {
  // 获取数据
  loading.value = true
  getObj(id).then((res: any) => {
    Object.assign(form, res.data)
  }).finally(() => {
    loading.value = false
  })
};

// 暴露变量
defineExpose({
  openDialog
});
</script>